package com.longrise.common.datasource.remote;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.loaddata.newloaddata.LoadDataManager4;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.android.loaddata.newloaddata.LoadDataUtil;

import java.io.File;
import java.util.Map;

/**
 * Created by callxkj on 2017/10/27.
 * token过期 重新登录
 */

public class ReloginUtil extends LoadDataUtil {

    private static final String TAG = "ReloginUtil";

    @Override
    public void loginFinish(EntityBean loginAgainBean, int type, String key, String serviceUrl,
                            String serviceName, EntityBean tempEntityBean, boolean openCache,
                            String fileKey, File file, String mimeType, Map headMap,
                            LoadDataManagerFather.OnuploadFileLishtener onuploadFileLishtener,
                            LoadDataManagerFather.OnRequestCompleteListener onRequestCompleteListener) {

        if (loginAgainBean != null) {
            Integer resultstate = loginAgainBean.getInt("resultstate", 0);
            if(resultstate == 1){
                EntityBean resultBean = loginAgainBean.getBean("result");
                String apptoken = resultBean.getString("apptoken");
                //修改请求参数的token
                LoadDataManager.getInstance().setToken(apptoken);
                //更换新的token后重新发起A请求
                LoadDataManager.getInstance().callServiceRelogin(key,serviceUrl,serviceName,
                        tempEntityBean,onRequestCompleteListener);
                return ;
            }
        }

        //失败时调用回调接口
        if(onRequestCompleteListener !=null)
        {
            onRequestCompleteListener.onError(null, serviceName, LoadDataManagerFather.ResultType.Failure.Failure);
            onRequestCompleteListener.onFinished(key, serviceName);
        }


    }
}
